<?php

namespace App\Http\Admin\Services;

use App\Exceptions\InvalidRequestException;
use App\Http\Admin\Models\Admin;
use Illuminate\Support\Facades\DB;

class AdminService extends BaseService
{
    private $admin;
    public function __construct(Admin $admin)
    {
        $this->admin = $admin;
    }

    public function list(){
        $result = $this->admin->select(['id','username','name'])->with('roles')->orderBy('id','desc')->paginate(20);
        return $result;
    }

    public function del($ids = []){
        if(!is_array($ids)){
            throw new InvalidRequestException('请传数组');
        }
        if(count($ids) == 0){
            throw new InvalidRequestException('缺少id');
        }
        $result = $this->admin->whereIn('id',$ids)->delete();
        return $result;
    }

    public function store($attributes = [])
    {
        //参数校验
        $this->getValidAttributes($attributes);
        $salt = getRandomStr(10);
        $password = $this->generatePassword($attributes['password'],$salt);

        DB::beginTransaction();
        try{
            //修改过程
            if(!empty($attributes['id'])){
                $admin = $this->admin->where('id',$attributes['id'])->first();
                $admin->username =  $attributes['username'];
                $admin->salt =  $salt;
                $admin->password =  $password;
                $admin->name =  $attributes['name'];

                $admin->save();


            }else{
                //添加过程
                $admin = $this->admin->create([
                    'username' => $attributes['username'],
                    'salt' => $salt,
                    'password' => $password,
                    'name' => $attributes['name']
                ]);

            }
            $admin->roles()->sync($attributes['role_ids']);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            throw new InvalidRequestException('数据存储错误');
        }


        return $admin;
    }

    public function getValidAttributes($attributes)
    {

        if (empty($attributes['username'])) {
            throw new InvalidRequestException('请填写账号');
        }

        if (empty($attributes['name'])) {
            throw new InvalidRequestException('请填写名字');
        }
        if (empty($attributes['password'])) {
            throw new InvalidRequestException('请填写密码');
        }
        if (strlen($attributes['password']) < 6) {
            throw new InvalidRequestException('密码必须大于6位');
        }



        if(!empty($attributes['id'])){
            $exist = $this->admin->where('username',$attributes['username'])
                ->where('id','!=',$attributes['id'])->exists();
            if($exist){
                throw new InvalidRequestException('账号已存在');
            }
        }else{
            $exist = $this->admin->where('username',$attributes['username'])->exists();
            if($exist){
                throw new InvalidRequestException('账号已存在');
            }
        }

    }

}
